
package  com.blx.utils;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * <p>StringUtility</p>
 * <p>
 * Description:
 * extends the StringsUtils of commons-lang3
 * </p>
 *
 * @since 1.0
 * @version 1.0, 2012-11-29
 * @author zhangxingkai
 */
public class StringUtility extends StringUtils {

	/**
     * <p>{@code StringUtility} instances should NOT be constructed in
     * standard programming. Instead, the class should be used as
     * {@code StringUtility.trim(" foo ");}.</p>
     *
     * <p>This constructor is public to permit tools that require a JavaBean
     * instance to operate.</p>
     */
	public StringUtility() {
		super();
	}

	/**
	 * <p>Removes control characters, including whitespace, from both ends
	 * of this string, handling null by returning an empty string.</p>
	 *
	 * @deprecated please Use {@code StringUtility.trimToEmpty()} for instead.
	 * @param str the String to be trimmed, may be {@code null}
	 * @return the trimmed String, or an empty String if {@code null} input
	 * @since 1.0
	 */
	@Deprecated
	public static String clean(String str) {
		// TDOO need delete
		return trimToEmpty(str);
	}

	/**
	 * <p>Concatenates elements of an array into a single string. The difference
	 * from join is that concatenate has no delimiter.</p>
	 *
	 * @deprecated please Use {@code StringUtility.join(T... elements)} for instead.
	 * @param <T> the specific type of values to join together
	 * @return the joined concatenated, {@code null} if null array input
	 * @since 1.0
	 */
	@Deprecated
	public static String concatenate(Object[] array) {
		return join(array, "");
	}

	/**
	 * <p>Checks if the email string is a legal Email.</p>
	 *
	 * @param email the email string to check
	 * @return true or false
	 */
	public static boolean isEmail(String email) {
		if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
			return false;
		}
		return true;
	}

	/**
	 * <p>Checks if the CharSequence is an IPv4 Address.</p>
	 *
	 * @param cs the IPv4 Address CharSequence to check
	 * @return true or false
	 */
	public static boolean isIPAddr(String ipAddr) {
		if (isBlank(ipAddr)) {
			return false;
		}

		String[] ipValue = split(ipAddr, ".");
		int length = ipValue.length;
		if (length != 4) {
			return false;
		}

		for (int i = 0; i < length; i++) {
			String tempStr = ipValue[i];
			if (!isNumeric(tempStr)) {
				return false;
			}
			int tempInt = Integer.parseInt(tempStr);
			if (tempInt < 0 || tempInt > 255) {
				return false;
			}
		}
		return true;
	}
	/**
	 * TODO 去除首尾中间空格 <br/>
	 * @param input
	 * @return
	 * @author wangzhang
	 * 2013-3-19 下午2:41:45
	 */
	public static String clearEmpty(String input){
		return input.replaceAll(" ", "");
	}



	/**
     * 转义like语句中的_和%
     */
    public static String escapeSQLLike(String likeStr) {
    	String str = StringUtils.replace(likeStr, "/",    "//");
        str = StringUtils.replace(str, "_", "/_");
        str = StringUtils.replace(str, "%",    "/%");

        return str;
    }

    @SuppressWarnings("rawtypes")
	public static String getMethodShortName(Method method){
    	Class clazz = method.getClass();
    	String packageName = clazz.getPackage().getName();
//		String className = clazz.getName().replaceAll(packageName + ".", "").split("\\$\\$")[0];
		String execution = method.toString();
		String methodName = execution.substring(10, execution.length() - 1);
		if(methodName.indexOf("(")>=0){
			methodName=methodName.substring(0,methodName.indexOf("("));
		}
		String[] tmp = methodName.split("\\.");
		if(tmp.length==1){
			methodName=tmp[0];
		}else{
			methodName=tmp[tmp.length-2]+"."+tmp[tmp.length-1];
		}
		return methodName;
    }

	// TODO convertHTML, escape, parseStringToMoney
	// TODO escapeSQLike, getStrByteLen, formatExcelString
    public static String getDateByTime(long time){
    	Date date = new Date(time);
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	
    	return format.format(date);
    }
    public static String getDateByStrTime(String time){
    	if(time == null || "".equals(time))
    		return "";
    	return getDateByTime(Long.valueOf(time));
    }
    public static void main(String[] args) {
		System.out.println(StringUtility.getDateByTime(1381724597830l));
	}
}
